<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @SWG\Definition(
 *      definition="WechatUser",
 *      required={"nickname", "openid", "unionid", "headimgurl"},
 *      @SWG\Property(
 *          property="id",
 *          description="id",
 *          type="integer",
 *          format="int32"
 *      ),
 *      @SWG\Property(
 *          property="nickname",
 *          description="nickname",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="openid",
 *          description="openid",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="unionid",
 *          description="unionid",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="language",
 *          description="language",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="city",
 *          description="city",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="province",
 *          description="province",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="country",
 *          description="country",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="headimgurl",
 *          description="headimgurl",
 *          type="string"
 *      ),
 *      @SWG\Property(
 *          property="subscribe_time",
 *          description="subscribe_time",
 *          type="integer",
 *          format="int32"
 *      ),
 *      @SWG\Property(
 *          property="status",
 *          description="status",
 *          type="integer",
 *          format="int32"
 *      ),
 *      @SWG\Property(
 *          property="updated_at",
 *          description="updated_at",
 *          type="string",
 *          format="date-time"
 *      ),
 *      @SWG\Property(
 *          property="created_at",
 *          description="created_at",
 *          type="string",
 *          format="date-time"
 *      )
 * )
 */
class WechatUser extends Model
{
    use SoftDeletes;

    public $table = 'wechat_users';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'nickname',
        'phone',
        'appid',
        'openid',
        'unionid',
        'sex',
        'language',
        'city',
        'province',
        'country',
        'headimgurl',
        'subscribe',
        'subscribe_time'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'nickname' => 'string',
        'phone' => 'string',
        'appid' => 'string',
        'openid' => 'string',
        'unionid' => 'string',
        'language' => 'string',
        'city' => 'string',
        'province' => 'string',
        'country' => 'string',
        'headimgurl' => 'string',
        'subscribe_time' => 'integer',
        'status' => 'integer'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'nickname' => 'required',
        'phone' => 'required',
        'headimgurl' => 'required',
        'country' => 'required',
        'province' => 'required',
        'city' => 'required',
    ];

    public function hasChannel()
    {
        return $this->hasOne('App\Models\Channel', 'id', 'channel_id')->select(['id', 'name', 'short_name', 'logo', 'linkman_name', 'linkman_phone', 'linkman_wechat', 'linkman_duty']);
    }
}